import { defineStore } from 'pinia'
import { reqLogin,reqUserInfo } from '@/api/user'
import type { loginFormData, loginResponseData } from '@/api/user/type.ts'
import type { stateTypes } from '@/store/types/stateType.ts'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token.ts'
import { constantRoute } from '@/router/routes'
let useUserStore = defineStore('User', {
  state: (): stateTypes => {
    return {
      token: GET_TOKEN(),
      menuRoutes: constantRoute,
      username:'',
      avatar:''
    }
  },
  actions: {
    async userLogin(data: loginFormData) {
      console.log(data)
      let res: loginResponseData = await reqLogin(data)
      console.log(res)
      if (res.code === 200) {
        this.token = res.data.token as string
        SET_TOKEN(res.data.token as string)
        // localStorage.setItem('TOKEN', res.data.token as string)
        return 'ok'
      } else {
        return Promise.reject(new Error(res.data.message))
      }
    },
   async  userInfo(){
      console.log(123)
      const res =  await reqUserInfo()
     console.log(res)
     if(res.code ===200){
       this.username = res.data.checkUser.username
       this.avatar = res.data.checkUser.avatar
     }
     return res
    }
  },
  getters: {},
})

export default useUserStore
